var codeList = [];

app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http){
    console.log('------$scope.menuId1-----', $scope.menuId);
    console.log('------$scope.buttonArr1-----', $scope.buttonArr);

    $scope.bindBtn = function(event) {
        var datas = $scope.getSelectedRows();
        var t = this;
        switch(event){
            case 'event-to-detail'://详情
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else if(datas.length > 1) {
                    layer.alert('只能选中一行数据');
                } else {
                    window.top.add_tab('Listing库('+datas[0].code+') - 详情', '/web/business/tpProductActive/toEdit?id=' + datas[0].id+"&editType=detail")
                }
                break;
            }
            case 'event-to-operationsEdit'://运营维护
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else if(datas.length > 1) {
                    layer.alert('只能选中一行数据');
                } else {
                    window.top.add_tab('Listing库('+datas[0].code+') - 运营维护', '/web/business/tpProductActive/toEdit?id=' + datas[0].id+"&editType=operations");
                }
                break;
            }
            case 'event-to-storageEdit'://仓储维护
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else if(datas.length > 1) {
                    layer.alert('只能选中一行数据');
                } else {
                    window.top.add_tab('Listing库('+datas[0].code+') - 仓储维护', '/web/business/tpProductActive/toEdit?id=' + datas[0].id+"&editType=storage");
                }
                break;
            }
            case 'event-to-logisticsEdit'://物流维护
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else if(datas.length > 1) {
                    layer.alert('只能选中一行数据');
                } else {
                    window.top.add_tab('Listing库('+datas[0].code+') - 物流维护', '/web/business/tpProductActive/toEdit?id=' + datas[0].id+"&editType=logistics");
                }
                break;
            }
            case 'event-to-purchaseEdit'://采购维护
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else if(datas.length > 1) {
                    layer.alert('只能选中一行数据');
                } else {
                    window.top.add_tab('Listing库('+datas[0].code+') - 采购维护', '/web/business/tpProductActive/toEdit?id=' + datas[0].id+"&editType=purchase");
                }
                break;
            }
            case 'event-to-manageEdit'://管理维护
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else if(datas.length > 1) {
                    layer.alert('只能选中一行数据');
                } else {

                    window.top.add_tab('Listing库('+datas[0].code+') - 管理维护', '/web/business/tpProductActive/toEdit?id=' + datas[0].id+"&editType=manage");
                }
                break;
            }
            case 'event-to-updateVgsbm'://批量修改运营公司编码
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else {
                    codeList = [];
                    for(var i=0;i<datas.length;i++){
                        codeList.push(datas[i].code);
                    }
                    //layui.YsModal.init({modalKey: 'THR_OperateCompanyApply', colCode: 'aaaa', parentObj: window});
                    $scope.showModalRadio("THR_OperateCompanyApply", 3, 'aaaa', null, {});
                    break;
                }
                break;
            }
            case 'event-to-export'://导出
            {
                $scope.showModalLink('/web/business/tpYsbExportLog/toExport?menuId=10184', '导出表格');
                break;
            }
            case 'LAYTABLE_EXPORT_DATA'://生成导出数据
            {
                $scope.doExport('/api/inter/tpProductActive/export');
                break;
            }
            case 'event-to-printGoods': // 打印商品标
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else if(datas.length > 1) {
                    layer.alert('只能选中一行数据');
                } else {
                    $http.post('/api/inter/tpProductActive/generateGoodsPDF', {}, {params: {id: datas[0].id}})
                        .success(function (data) {
                            if (data.code == 0) {
                                if(data.msg != undefined && data.msg.startsWith('http')) {
                                    window.open(data.msg);
                                } else {
                                    layer.msg(data.msg);
                                }
                            } else {
                                alert(data.msg);
                            }
                        })
                        .error(function(result){
                            layer.close();
                            layer.msg("删除失败，请稍后再试！", {icon: 2});
                        });

                }
                break;
            }


            case 'event-to-copyGoods': // 商品复制
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else {
                    var codeList = [];
                    for(var i=0;i<datas.length;i++){
                        codeList.push(datas[i].code);
                    }
                    var codes = '';
                    for(var i=0;i<codeList.length;i++){
                        if(codes==''){
                            codes = codeList[i];
                        }else {
                            codes = codes+','+codeList[i];
                        }
                    }
                    layui.layer.confirm("确定要复制所选的商品？",{
                        btn: ['确定', '取消']
                    }, function (index) {
                        layui.layer.msg('处理中', {
                            icon: 16,
                            shade: 0.01
                        });
                        $http.post('/api/inter/tpProductActive/copyGoods', {}, {params: {codes: codes}})
                            .success(function (data) {
                                if (data.code == 0) {
                                    layer.msg('操作成功！');
                                    setTimeout(function(){
                                        layer.closeAll();
                                        location.reload();
                                    }, 3000);
                                } else {
                                    layer.msg(data.msg);
                                }
                            })
                            .error(function(result){
                                layer.close();
                                layer.msg("操作失败，请稍后再试！", {icon: 2});
                            });
                    });


                }
                break;
            }


        };
    }
}

//表格双击
app.controller('dbClickCtrl', ['$scope', '$http', DbClickController]);
function DbClickController($scope, $http){
    $scope.doubleClick = function(data) {
        console.log('------data-----', data);
        window.top.add_tab('Listing库('+data.code+') - 详情', '/web/business/tpProductActive/toEdit?id=' + data.id+"&editType=detail");
    }
}


/*
 * 自定义通用控制器，若无定义，页面会报错
 */
app.controller('mainSelfCtrl', ['$scope', '$http', function GridSelfController($scope, $http){

    $scope.doSelectRadioBySelf = function(data) {
        console.log('单选框特殊回调', data);
        layui.layer.msg('处理中', {
            icon: 16,
            shade: 0.01
        });
        var codes = '';
        for(var i=0;i<codeList.length;i++){
            if(codes==''){
                codes = codeList[i];
            }else {
                codes = codes+','+codeList[i];
            }
        }
        $http.post('/api/inter/tpProductActive/updateVgsbm', {}, {params: {codeList: codes,vgsbm:data.vgsbm}})
            .success(function (data) {
                if (data.code == 0) {
                    layer.msg('修改成功！');
                } else {
                    layer.msg(data.msg);
                }
                setTimeout(function(){
                    layer.closeAll();
                    location.reload();
                }, 3000);
            })
            .error(function(result){
                layer.close();
                layer.msg("删除失败，请稍后再试！", {icon: 2});
            });

    }
}]);
